package finbudgettable.client.ui.grid.cell.currency;

import java.math.BigDecimal;
import java.util.List;

import com.google.gwt.user.cellview.client.Header;

import finbudget.client.ui.table.Record;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.footer.FooterBigDecimalCell;

public class CurrencyFooter extends Header<BigDecimal> {

	private final CellGrid grid;
	private final String fieldId;

	public CurrencyFooter(CellGrid grid, String fieldId) {
		super(new FooterBigDecimalCell());

		this.grid = grid;
		this.fieldId = fieldId;
	}

	@Override
	public BigDecimal getValue() {
		List<Record> records = grid.getFilteredRecords();
		if (records != null) {
			BigDecimal result = BigDecimal.ZERO;
			for (Record record : records) {
				BigDecimal value = record.getValueBigDecimal(fieldId);
				if (value != null) {
					result = result.add(value);
				}
			}
			return result;
		}
		return null;
	}

}
